package com.itmate.wei.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.itmate.wei.db.DBUtil;
import com.itmate.wei.entity.Family;
import com.itmate.wei.entity.Member;
import com.itmate.wei.entity.Statues;

public class StatuesDao extends BaseDao{

	
	
	public List<Statues> SelectStaBydate(String date){
	List list=new ArrayList<Statues>();
	try {
		connection=DBUtil.getConnection();
		preparedStatement=connection.prepareStatement("select * from account where date like ?");
		preparedStatement.setString(1, "%"+date+"%");
		resultSet=preparedStatement.executeQuery();
		while(resultSet.next()){
			Statues sta = new Statues();
			int id=resultSet.getInt("id");
			sta.setId(id);
			sta.setStatue(resultSet.getString("statue"));
			sta.setMoney(resultSet.getDouble("money"));
			sta.setDate(resultSet.getString("date"));
			sta.setFamily_id(resultSet.getInt("family_id"));
			sta.setMember_id(resultSet.getInt("member_id"));
			list.add(sta);
		}
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally{
		closed();
	}
	return list;
	
}
	
	public int addStatues(Statues sta){
		int count = 0;
	 try {
		connection = DBUtil.getConnection();
		String sql = "insert into account(statue,money,date,family_id,member_id) values(?,?,?,?,?)";
		preparedStatement = connection.prepareStatement(sql);
		preparedStatement.setString(1, sta.getStatue());
		preparedStatement.setDouble(2, sta.getMoney());
		preparedStatement.setString(3, sta.getDate());
		preparedStatement.setInt(4, sta.getFamily_id());
		preparedStatement.setInt(5, sta.getMember_id());
		count = preparedStatement.executeUpdate();
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally{
		closed();
	}
	return count;
}
	public int UpdataStatues(Statues sta){
		int i=-1;
		try {
			connection=DBUtil.getConnection();
			preparedStatement=connection.prepareStatement("update account set statue =?,money=?,date=?,family_id=?,member_id=? where id=?");
			
			preparedStatement.setString(1, sta.getStatue());
			preparedStatement.setDouble(2, sta.getMoney());
			preparedStatement.setString(3, sta.getDate());
			preparedStatement.setInt(4, sta.getFamily_id());
            preparedStatement.setInt(5, sta.getMember_id());
            preparedStatement.setInt(6, sta.getId());
			i=preparedStatement.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return i;
		
	}
	
	public int DeleteByID(int id){
		int i=-1;
		try {
			connection=DBUtil.getConnection();
			preparedStatement=connection.prepareStatement(
					"delete from account where id=?");
			preparedStatement.setInt(1, id);
			i=preparedStatement.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return i;
	}
	
	public List<Statues> SelectAllUser(){
		List<Statues> statuelist=new ArrayList<>();
		try {
			connection = DBUtil.getConnection();
			String sql = "select * from account";
			statement = connection.createStatement();
			resultSet =statement.executeQuery(sql);
			while(resultSet.next()){
				Statues sta = new Statues();
				sta.setId(resultSet.getInt("id"));
				sta.setStatue(resultSet.getString("statue"));
				sta.setMoney(resultSet.getDouble("money"));
				sta.setDate(resultSet.getString("date"));
				sta.setFamily_id(resultSet.getInt("family_id"));
				sta.setMember_id(resultSet.getInt("Member_id"));
				statuelist.add(sta);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			closed();
		}
		
		return statuelist;
	}
	public List<Family> SelectAllFamily(){
		List<Family> familylist=new ArrayList<>();
		try {
			connection = DBUtil.getConnection();
			String sql = "select * from family";
			statement = connection.createStatement();
			resultSet =statement.executeQuery(sql);
			while(resultSet.next()){
				Family f = new Family();
				f.setId(resultSet.getInt("id"));
				f.setName(resultSet.getString("name"));
				familylist.add(f);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			closed();
		}
		
		return familylist;
	}
	public List<Member> SelectAllMember(){
		List<Member> memberlist=new ArrayList<>();
		try {
			connection = DBUtil.getConnection();
			String sql = "select * from home_member";
			statement = connection.createStatement();
			resultSet =statement.executeQuery(sql);
			while(resultSet.next()){
				Member m = new Member();
				m.setId(resultSet.getInt("id"));
				m.setName(resultSet.getString("name"));
				m.setRelation(resultSet.getString("relation"));
				memberlist.add(m);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			closed();
		}
		
		return memberlist;
	}

}
